<?php /* PROJECTS $Id: addedit.php,v 1.93.6.9 2007/02/02 18:11:04 merlinyoda Exp $ */
$project_id = intval( dPgetParam( $_GET, "project_id", 0 ) );
$company_id = intval( dPgetParam( $_GET, "company_id", 0 ) );
$contact_id = intval( dPgetParam( $_GET, "contact_id", 0 ) );

$perms =& $AppUI->acl();
// check permissions for this record
$canEdit = $perms->checkModuleItem( $m, 'edit', $project_id );
$canAuthor = $perms->checkModuleItem( $m, 'add' );
if ((!$canEdit && $project_id > 0) || (!$canAuthor && $project_id == 0)) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect();
}

// get a list of permitted companies
require_once( $AppUI->getModuleClass ('companies' ) );
require_once( $AppUI->getModuleClass ('suppliers' ) );
require_once( $AppUI->getMasterDataClass ('mdtype' ) );
require_once( $AppUI->getMasterDataClass ('mdstatus' ) );
require_once( $AppUI->getMasterDataClass ('mdcurrency' ) );

$row = new CCompany();
$companies = $row->getAllowedRecords( $AppUI->user_id, 'company_id,company_name', 'company_name' );
$companies = arrayMerge( array( ''=>'' ), $companies );

$suppliers = getSupplierList();

$row = new CMdType();
$project_types = $row->getTypeList('Reference');

$project_shipping_types = $row->getTypeList('ShippingType');

$row = new CMdCurrency();
$currencyArr = $row->getCurrencyList();

$row = new CMdStatus();
$project_statuses = $row->getStatusList('Reference');
unset($project_statuses['']);//remove empty status
//exclude complete status
$complete_status = new CMDStatus();
$project_completed = dPgetSysVal( 'ProjectCompleted' );
$complete_status->loadByCode($project_completed[0]);
unset($project_statuses[$complete_status->status_id]);
$project_completed = dPgetSysVal( 'ProjectProcessed' );
$complete_status->loadByCode($project_completed[0]);
unset($project_statuses[$complete_status->status_id]);
// trangdt: remove complete status
require_once(DP_BASE_DIR."/modules/masterdata/mdstatus.class.php");
$complete_status = new CMDStatus();
$project_completed = dPgetSysVal( 'ProjectCompleted' );
$complete_status->loadByCode($project_completed[0]);
unset($project_completed[$complete_status->status_id]);
// trangdt end

// pull users
$q  = new DBQuery;
$q->addTable('users','u');
$q->addTable('contacts','c');
$q->addQuery('user_id');
$q->addQuery('CONCAT_WS(" ",contact_first_name,contact_last_name)');
$q->addOrder('contact_last_name');
$q->addWhere('u.user_contact = c.contact_id');
$q->addWhere('user_id <> 1');
$users = $q->loadHashList();

// load the record data
$row = new CProject();

if ($project_id > 0 && !$row->load( $project_id, false )) {
	$AppUI->setMsg( 'Project' );
	$AppUI->setMsg( "invalidID", UI_MSG_ERROR, true );
	$AppUI->redirect();
} else if (count( $companies ) < 2 && $project_id == 0) {
	$AppUI->setMsg( "noCompanies", UI_MSG_ERROR, true );
	$AppUI->redirect();
}
// trangdt add
$canEdit = $row->canEdit();
if (!$canEdit) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect();
}
// trangdt end
if ($project_id == 0 && $company_id > 0) {
	$row->project_company = $company_id;
}

// add in the existing company if for some reason it is dis-allowed
if ($project_id && !array_key_exists( $row->project_company, $companies )) {
	$q  = new DBQuery;
	$q->addTable('companies');
	$q->addQuery('company_name');
	$q->addWhere('companies.company_id = '.$row->project_company);
	$companies[$row->project_company] = $q->loadResult();
}

// get critical tasks (criteria: task_end_date)
$criticalTasks = ($project_id > 0) ? $row->getCriticalTasks() : NULL;

// get ProjectPriority from sysvals
$projectPriority = dPgetSysVal( 'ProjectPriority' );

// format dates
$df = $AppUI->getPref('SHDATEFORMAT');

$start_date = new CDate( $row->project_start_date );
$reference_date = new CDate( $row->project_date );
$end_date = intval( $row->project_end_date ) ? new CDate( $row->project_end_date ) : null;
$actual_end_date = intval( $criticalTasks[0]['task_end_date'] ) ? new CDate( $criticalTasks[0]['task_end_date'] ) : null;
$style = (( $actual_end_date > $end_date) && !empty($end_date)) ? 'style="color:red; font-weight:bold; width: 70px"' : '';

// setup the title block
$titleBlock = new CTitleBlock( $project_id > 0 ? 'Edit Reference' : 'New Reference' );
$titleBlock->addButton( 'references list', 'index.php?m=projects' );
$titleBlock->addCell();
$titleBlock->show();

//Build display list for departments
$company_id = $row->project_company;

// Build Project member list
$project_contacts = $project_id > 0 ? $row->getMembers() :
	array($AppUI->user_id => $AppUI->user_first_name.' '.$AppUI->user_last_name);

$project_status = $project_id > 0 ? $row->project_status : $_REQUEST['project_status'];
?>
<script language="javascript">
Calendar.enabled = true;

function setColor(color) {
	var f = document.editFrm;
	if (color) {
		f.project_color_identifier.value = color;
	}
	$('test').style.background = '#' + f.project_color_identifier.value;
	//fix for mozilla: does this work with ie? opera ok.
}

function setShort() {
	var f = document.editFrm;
	var x = 10;
	if (f.project_name.value.length < 11) {
		x = f.project_name.value.length;
	}
	if (f.project_short_name.value.length == 0) {
		f.project_short_name.value = f.project_name.value.substr(0,x);
	}
}

function doSubmit() {
	var f = document.editFrm;
	var msg = '';

	f.project_name.value = f.project_name.value.trim();
	//f.project_short_name.value = f.project_short_name.value.trim();

	<?php
	/*
	** Automatic required fields generated from System Values
	*/
	$requiredFields = dPgetSysVal( 'ProjectRequiredFields' );
	echo dPrequiredFields($requiredFields);
	?>

	if (msg.length < 1) {
		MemberList('idMembers').selectAll();
		f.submit();
	} else {
		alert(msg);
	}
}

function doCancel()
{
	if (confirm('Are you sure you want to cancel.')) {
		go('?<?=$AppUI->getPlace()?>');
	}
}

function changeManager(list)
{
	var mlist = $('idMembers');
	var alist = $('idAvailables');
	var i, m = list.options[list.selectedIndex].value;
	for (i = 0; i < mlist.options.length; i++)
		if (mlist.options[i].value == m) {
			break;
		}

	if (i >= mlist.options.length) {
		mlist.options[i] = new Option(list.options[list.selectedIndex].text, m);
	}

	for (i = 0; i < alist.options.length; i++)
		if (alist.options[i].value == m) {
			alist.options[i] = null;
			break;
		}
}
</script>
<form name="editFrm" action="index.php?m=projects" method="post">
	<input type="hidden" name="dosql" value="do_project_aed" />
	<input type="hidden" name="project_id" value="<?=$project_id;?>" />
	<input type="hidden" name="project_creator" value="<?=($project_id > 0) ? $row->project_creator : $AppUI->user_id;?>" />
	<input type="hidden" name="project_code" value="<?=@$row->project_code;?>" />
	<input type="hidden" name="project_iso_code" value="<?=@$row->project_iso_code;?>" />
<table cellspacing="0" cellpadding="4" border="0" width="100%">
<tr>
	<td class="tabon">Reference <?=$row->project_name?> Information</td>
	<td width="100%">&nbsp;</td>
</tr>
</table>
<table cellspacing="0" cellpadding="4" border="0" width="100%" class="infopanel">
<tr>
	<td>
		<?
		if ($project_id > 0) {
			showButton( "view", "?m=projects&a=view&project_id=$project_id" );
		}
		?>
		<input class="button" type="submit" name="btnFuseAction2" value="<?=$AppUI->_('save');?>" />
		<input class="button" type="button" name="cancel2" value="<?=$AppUI->_('cancel');?>" onClick="doCancel()" />
	</td>
	<td align="right">
		<?php
		if ($project_id == 0) {
			$q  = new DBQuery();
			$q->addTable('projects', 'p');
			$q->addTable('tasks', 't');
			$q->addQuery('p.project_id, p.project_name');
			$q->addWhere('t.task_project = p.project_id');
			$q->addWhere('p.project_status = 7');
			$q->addOrder('p.project_name');

			$importList = $q->loadHashList ();
			if (count($importList)) {
				echo $AppUI->_('Import tasks from').':';
				$importList = arrayMerge( array( '0'=> $AppUI->_('None') ), $importList);
				echo arraySelect( $importList, 'import_tasks_from', 'size="1" class="text"', null, false );
			}
		}
		?>
	</td>
</tr>
</table>
<table cellspacing="0" cellpadding="4" border="0" width="100%" class="subinfopanel">
<tr>
	<td valign="top">
		<table cellspacing="0" cellpadding="2" border="0">
		<tr>
			<td align="right" nowrap="nowrap">* <?=$AppUI->_('Name')?></td>
			<td colspan="3">
				<input id="project_name" type="text" name="project_name" value="<?=dPformSafe( $row->project_name );?>"
					style="width: 98%" maxlength="50" class="text" title="Reference name:: Reference name is required"/>
			</td>
		</tr>
		<tr>
			<td align="right" nowrap="nowrap">* <?=$AppUI->_('Type');?></td>
			<td colspan="3">
				<?=arraySelect( $project_types, 'project_rtype', 'id="project_rtype" title="Reference type :: Reference type is require" style="width: 100%" size="1" class="text"', $row->project_rtype, true );?>
			</td>
		</tr>
		<tr>
			<td align="right" nowrap="nowrap">* <?=$AppUI->_( 'Status' );?></td>
			<td colspan="3">
				<?=arraySelect( $project_statuses, 'project_rstatus', 'id="project_rstatus" title="Reference status :: Status is required" size="1" class="text" style="width: 100%"', $row->project_rstatus, true );?>
			</td>
		</tr>
		<tr>
			<td align="right" nowrap="nowrap">* <?=$AppUI->_('Reference date');?></td>
			<td nowrap="nowrap">
				<input id="idDateReference" type="hidden" name="project_date" value="<?=$reference_date->format( FMT_TIMESTAMP_DATE );?>" />
				<input id="idDateReferenceD" type="text" class="text" name="start_date"
					value="<?=$reference_date->format( $df );?>" readonly="readonly"
					style="width: 70px" title="Reference date :: This field is required"/>
				<img id="idDateReferenceB" src="images/calendar.gif" title="<?=$AppUI->_('Calendar');?>" align="absmiddle"/>
			</td>
			<td align="right" nowrap="nowrap"><?=$AppUI->_('Start Date');?></td>
			<td nowrap="nowrap">
				<input id="idDatePStart" type="hidden" name="project_start_date" value="<?=$start_date->format( FMT_TIMESTAMP_DATE );?>" />
				<input id="idDatePStartD" type="text" class="text" name="start_date"
					value="<?=$start_date->format( $df );?>" readonly="readonly"
					style="width: 70px" title="Reference start date :: Start date is required"/>
				<img id="idDatePStartB" src="images/calendar.gif" title="<?=$AppUI->_('Calendar');?>" align="absmiddle"/>
			</td>
		</tr>
		<tr style="display: none">
			<td align="right" nowrap="nowrap"><?=$AppUI->_('Actual Budget');?> <?=$dPconfig['currency_symbol'] ?></td>
			<td>
				<input type="text" name="project_actual_budget" value="<?=@$row->project_actual_budget;?>" size="10" maxlength="10" class="text"/>
			</td>
		</tr>
		</table>
	</td>
	<td valign="top" width="100%">
		<table cellspacing="0" cellpadding="2" border="0" width="100%">
		<tr>
			<td align="right" nowrap="nowrap">* <?=$AppUI->_('Customer');?></td>
			<td width="90%" nowrap="nowrap">
			<?=arraySelect( $companies, 'project_company', 'id="project_company" title="Customer :: Customer is required" class="text" size="1" style="width: 275px"', $row->project_company );?>
			</td>
		</tr>
		<tr>
			<td align="right" nowrap="nowrap">* <?=$AppUI->_('Shipper');?></td>
			<td nowrap="nowrap">
			<?=arraySelect( $suppliers, 'project_shipper', 'id="project_shipper" title="Shipper :: Shipper is required" class="text" size="1" style="width: 275px"', $row->project_shipper );?>
			</td>
		</tr>
		<tr>
			<td align="right" nowrap="nowrap">* <?=$AppUI->_('Shipping type');?></td>
			<td nowrap="nowrap">
			<?=arraySelect( $project_shipping_types, 'project_shipping_type', 'id="project_shipping_type" title="Shipping type :: Shipping type is required" class="text" size="1" style="width: 275px"', $row->project_shipping_type );?>
			</td>
		</tr>
		<tr>
			<td align="right" nowrap="nowrap">* <?=$AppUI->_('Currency');?></td>
			<td nowrap="nowrap">
			<?=arraySelect( $currencyArr, 'project_currency', 'id="project_currency" class="text" size="1" style="width: 275px"', $row->project_currency );?>
			</td>
		</tr>
		</table>
	</td>
	<!-- Hide project members -->
	<td width="100%" valign="top" style="visibility: hidden; display:none;">
		<table cellspacing="0" cellpadding="2" border="0" width="100%">
		<tr>
			<td align="right" nowrap="nowrap" width="50px"><?=$AppUI->_('Manager');?></td>
			<td width="100%">
			<?=arraySelect( $users, 'project_owner', 'size="1" class="text" style="width: 100%" onchange="changeManager(this)"',$row->project_owner ? $row->project_owner : $AppUI->user_id )?>
			</td>
		</tr>
		<tr style="display: none">
			<td colspan="2" width="100%">
				<table cellspacing="2" cellpadding="0" border="0" width="100%">
				<tr>
					<td>Reference Members</td>
					<td width="20px">&nbsp;</td>
					<td>Available Users</td>
				</tr>
				<tr>
					<td width="45%">
					<?=arraySelect( $project_contacts, 'project_contacts[]', 'id="idMembers" size="10" class="text" style="width: 100%; height: 150px" multiple="true"', 0) ?>
					</td>
					<td align="center" class="internal">
						<input type="image" src="images/prev.gif" class="button"
							onclick="UserList('idMembers', 'idAvailables').join(); return false;"/><br/><br/>
						<input type="image" src="images/next.gif" class="button"
							onclick="UserList('idMembers', 'idAvailables').part(); return false;"/>
					</td>
					<td width="44%">
					<?php
						foreach ($project_contacts as $id => $v) unset($users[$id]);
						echo arraySelect( $users, 'available_users', 'id="idAvailables" size="10" class="text" style="width: 100%; height: 150px" multiple="true"', 0)
					?>
					</td>
				</tr>
				</table>
			</td>
		</tr>
		<tr style="display: none">
			<td align="right" nowrap="nowrap"><?=$AppUI->_('Color Identifier');?></td>
			<td nowrap="nowrap">
				<input type="text" name="project_color_identifier" value="<?=(@$row->project_color_identifier) ? @$row->project_color_identifier : 'FFFFFF';?>" size="10" maxlength="6" onBlur="setColor();" class="text" /> *
			</td>
			<td nowrap="nowrap" align="right">
				<a href="#" onClick="newwin=window.open('./index.php?m=public&a=color_selector&dialog=1&callback=setColor', 'calwin', 'width=320, height=300, scrollbars=no');"><?php echo $AppUI->_('change color');?></a>
			</td>
			<td nowrap="nowrap">
				<span id="test" title="test" style="background:#<?=(@$row->project_color_identifier) ? @$row->project_color_identifier : 'FFFFFF';?>;"><a href="#" onClick="newwin=window.open('./index.php?m=public&a=color_selector&dialog=1&callback=setColor', 'calwin', 'width=320, height=300, scrollbars=no');"><img src="./images/shim.gif" border="1" width="40" height="20" /></a></span>
			</td>
		</tr>
		<input type="hidden" name="project_status" value="<?=$project_status?>"/>
		</table>
	</td>
</tr>
<tr>
	<td colspan="2">
		<table cellspacing="0" cellpadding="3" border="0">
			<tr>
			<td width="26px">&nbsp;</td>
			<td valign='top'><?=$AppUI->_('Description');?></td>
			<td colspan="100%">
				<textarea name="project_description" style="width: 630px; height: 50px" class="textarea"><?=dPformSafe( @$row->project_description );?></textarea>
			</td>
			</tr>
		</table>
	</td>
</tr>
</form>
</table>
<div id="errorMessage">
</div>
<div id="validation_rules" style="visibility: hidden; display: none">
	var fItemName = new LiveValidation('project_name');
	fItemName.add( Validate.Presence);

	var fProjectType = new LiveValidation('project_rtype');
	fProjectType.add( Validate.Presence);

	var project_rstatus = new LiveValidation('project_rstatus');
	project_rstatus.add( Validate.Presence);

	var project_date = new LiveValidation('idDateReferenceD');
	project_date.add( Validate.Presence);

	var project_start_date = new LiveValidation('idDatePStartD');
	project_start_date.add( Validate.Presence);

	var project_company = new LiveValidation('project_company');
	project_company.add( Validate.Presence);

	var project_shipper = new LiveValidation('project_shipper');
	project_shipper.add( Validate.Presence);

	var project_shipping_type = new LiveValidation('project_shipping_type');
	project_shipping_type.add( Validate.Presence);

	new Tips($$('.text'));
</div>
<script type="text/javascript" language="javascript">
	eval($('validation_rules').innerHTML);
</script>
